package com.sangfor.ssl.common;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.os.Handler;
import com.sangfor.bugreport.logger.Log;
import java.lang.ref.WeakReference;
import java.util.ArrayList;

/* loaded from: classes2.dex */
public class Foreground implements Application.ActivityLifecycleCallbacks {
    public static final long CHECK_DELAY = 2000;
    public static final String TAG = "Foreground";
    private static Foreground instance;
    private Runnable check;
    private Activity current;
    private boolean foreground;
    private ArrayList<Listener> listeners = new ArrayList<>(4);
    private Handler handler = new Handler();

    /* loaded from: classes2.dex */
    public interface Listener {
        void onBecameBackground();

        void onBecameForeground();
    }

    @Override // android.app.Application.ActivityLifecycleCallbacks
    public void onActivityCreated(Activity activity, Bundle bundle) {
    }

    @Override // android.app.Application.ActivityLifecycleCallbacks
    public void onActivityDestroyed(Activity activity) {
    }

    @Override // android.app.Application.ActivityLifecycleCallbacks
    public void onActivityResumed(Activity activity) {
    }

    @Override // android.app.Application.ActivityLifecycleCallbacks
    public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
    }

    public static Foreground init(Application application) {
        if (instance == null) {
            Foreground foreground = new Foreground();
            instance = foreground;
            application.registerActivityLifecycleCallbacks(foreground);
        }
        return instance;
    }

    public static Foreground get(Application application) {
        if (instance == null) {
            init(application);
        }
        return instance;
    }

    public static Foreground get() {
        Foreground foreground = instance;
        if (foreground != null) {
            return foreground;
        }
        throw new IllegalStateException("Foreground is not initialised - first invocation must use parameterised init/get");
    }

    public boolean isForeground() {
        return this.foreground;
    }

    public boolean isBackground() {
        return !this.foreground;
    }

    public void addListener(Listener listener) {
        synchronized (this) {
            this.listeners.add(listener);
        }
    }

    public void removeListener(Listener listener) {
        synchronized (this) {
            this.listeners.remove(listener);
        }
    }

    @Override // android.app.Application.ActivityLifecycleCallbacks
    public void onActivityPaused(Activity activity) {
        if (activity.isChangingConfigurations()) {
            return;
        }
        final WeakReference weakReference = new WeakReference(activity);
        Handler handler = this.handler;
        Runnable runnable = new Runnable() { // from class: com.sangfor.ssl.common.Foreground.1
            @Override // java.lang.Runnable
            public void run() {
                Foreground.this.onActivityCeased((Activity) weakReference.get());
            }
        };
        this.check = runnable;
        handler.postDelayed(runnable, CHECK_DELAY);
    }

    @Override // android.app.Application.ActivityLifecycleCallbacks
    public void onActivityStarted(Activity activity) {
        this.current = activity;
        Runnable runnable = this.check;
        if (runnable != null) {
            this.handler.removeCallbacks(runnable);
        }
        if (this.foreground || activity == null || activity.isChangingConfigurations()) {
            return;
        }
        this.foreground = true;
        Log.warn(TAG, "became foreground");
        dispatchBecameForeground();
    }

    @Override // android.app.Application.ActivityLifecycleCallbacks
    public void onActivityStopped(Activity activity) {
        Runnable runnable = this.check;
        if (runnable != null) {
            this.handler.removeCallbacks(runnable);
        }
        onActivityCeased(activity);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void onActivityCeased(Activity activity) {
        if (!this.foreground || activity != this.current || activity == null || activity.isChangingConfigurations()) {
            return;
        }
        this.foreground = false;
        Log.warn(TAG, "went background");
        dispatchBecameBackground();
    }

    private void dispatchBecameForeground() {
        synchronized (this) {
            if (this.listeners.isEmpty()) {
                return;
            }
            int size = this.listeners.size();
            Listener[] listenerArr = new Listener[size];
            this.listeners.toArray(listenerArr);
            for (int i = 0; i < size; i++) {
                listenerArr[i].onBecameForeground();
            }
        }
    }

    private void dispatchBecameBackground() {
        synchronized (this) {
            if (this.listeners.isEmpty()) {
                return;
            }
            int size = this.listeners.size();
            Listener[] listenerArr = new Listener[size];
            this.listeners.toArray(listenerArr);
            for (int i = 0; i < size; i++) {
                listenerArr[i].onBecameBackground();
            }
        }
    }
}
